#include <stdio.h>
#include <string.h>
#include <page_manager.h>

static PT_pageAction g_ptPageHead;

int registerPages(PT_pageAction pageAction)
{
	PT_pageAction me = g_ptPageHead;

	if(pageAction ==NULL)
	{
		fprintf(stderr,"parama error\n");
		return -1;
	}

	if(g_ptPageHead ==NULL)
	{
		g_ptPageHead = pageAction;
	}
	else
	{
		while(me->next !=NULL)
		{
			me = me->next;
		}
		me->next = pageAction;
	}

	return 0;
}

int initPages(void)
{
	int ret;
	
	//mainpage
	ret = mainpageInit();
	if(ret<0)
	{
		fprintf(stderr, "mainpageInit error\n");
		return ret;
	}

	//settingpage
	ret = settingpageInit();
	if(ret<0)
	{
		fprintf(stderr, "settingpageInit error\n");
		return ret;
	}

	return 0;
}

void showPages(void)
{
	int i=0;
	PT_pageAction me = g_ptPageHead;

	while(me !=NULL)
	{
		fprintf(stdout, "%02d %s\n", i++, me->name);
		me = me->next;
	}
}

int runPage(char *pageName)
{
	PT_pageAction me = g_ptPageHead;

	while(me !=NULL)
	{
		if(strcmp(me->name, pageName) ==0)
			me->run();
		me = me->next;
	}

	return -1;
}

